module.exports = function ($scope, $resource, $stateParams, FileUploader, toaster) {

  //进入页面默认false，false：事项流程，true：申请办理
  $scope.flag = $stateParams.flag=='1'?true:false;
  $scope.formdate = {
    'loading': true,
    'arealist': [],
    'card_type': [
      {'id': '1', 'name': '身份证', 'code': 'card1'},
      {'id': '2', 'name': '户口本', 'code': 'card2'},
      {'id': '3', 'name': '军人证', 'code': 'card3'},
      {'id': '4', 'name': '党员证', 'code': 'card4'},
      {'id': '5', 'name': '港澳通行证', 'code': 'card5'},
    ],
    'type_list': [
      {'id': '1', 'name': '城市低保', 'code': '100'},
      {'id': '1', 'name': '农村低保', 'code': '200'},
    ],
    'param_index': {}
  };
  if($scope.flag){
    var para = {
      'code': $stateParams.code
    }
    $resource('/api/as/csc/auditorder/orderlist', {}, {}).save(para, function(res){
			if (res.errcode === 0) {
        $scope.formdate.params = JSON.parse(res.data.results[0].params);
			} else {
				toaster.error({ title: '', body: '查询草稿事项失败'});
			}
		});
    
  }

  $scope.getarealist = function(){
    var para = {
      'pid': '',
      'idcardNumber': $stateParams.cardno
    }
    $resource('/api/ac/csc/createPostService/getAdministrativePlanning', {}, {}).save(para,function(res){
      if (res.data.rescode == '0000') {
        $scope.formdate.arealist = res.data.data.divisions;
      } else {
        toaster.error({title: '', body: '查询区域信息失败'});
      }
      init();
    });
  };
  $scope.getarealist();


  function init(){
    $resource('/api/ac/csc/auditItemService/itemInfo', {}, {}).save({'id': $stateParams.item_id},function (res) {
      if (res.errcode !== 0) {
        toaster.error({title: '', body: '查询项目信息失败'});
        return;
      }
      $scope.iteminfo = res.data;
      
      $scope.iteminfo.documentlist.forEach(element => {
        if($scope.flag){
          element.value1 = $scope.formdate.params[element.value];
        }
        if(element.write_type == 'images'){
          element.uploader1 = new FileUploader({
            url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=community&selfdir=img'
          });
          element.uploader1.filters.push({
            name: 'imageFilter',
            fn: function (item /*{File|FileLikeObject}*/, options) {
                var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
                return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
            }
          });
          element.uploader1.onSuccessItem = function (fileItem, response, status, headers) {
            element.value1 = response.savename;
          };
        } else if(element.write_type == 'form') {
          $resource('/api/as/csc/audititem/getdocumentbypidlist', {}, {}).save({'pid': element.document_id},function (res1) {
            if (res.errcode !== 0) {
              toaster.error({title: '', body: '加载项目信息失败，请刷新页面'});
              return;
            }
            var temp = {};
            if($scope.flag){
              temp = $scope.formdate.params[element.value];
            }
            element.param = res1.data;
            angular.forEach(element.param, function(e2, index, array){
              if($scope.flag){
                e2.value1 = temp[e2.value];
              }
              $scope.formdate.param_index[element.value+'_'+e2.id] = index;
              if(e2.write_type == 'select'){
                if(e2.value == 'param32'){
                  e2.select = $scope.formdate.card_type;
                } else if(e2.value == 'param34' || e2.value == 'param38' || e2.value == 'param42'){
                  e2.select = $scope.formdate.arealist;
                } else if(e2.value == 'param51'){
                  e2.select = $scope.formdate.type_list;
                }
              }
            });
          });
        }
      });
      $scope.formdate.loading = false;
      // console.log($scope.iteminfo);
    });
  }

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };

  $scope.ok = function(){
    var para = {
      'item_id': $scope.iteminfo.id,
      'params': {},
      'name': '',
      'mobile': '',
      'cardno': ''
    }
    $scope.iteminfo.documentlist.forEach(element => {
      if(element.write_type != 'form'){
        para.params[element.value] = element.value1;
      } else if(element.write_type == 'form'){
        para.params[element.value] = {};
        element.param.forEach(eee => {
          para.params[element.value][eee.value] = eee.value1;
          if('param31' == eee.value){
            para.cardno = eee.value1;
          } else if('param33' == eee.value){
            para.name = eee.value1;
          } else if('param47' == eee.value){
            para.mobile = eee.value1;
          }
        });
      }
    });
    $resource('/api/ac/csc/auditOrderService/createOrder', {}, {}).save(para,function (res) {
      if (res.errcode !== 0) {
        toaster.error({title: '', body: '保存失败'});
        return;
      }
      toaster.success({title: '', body: '成功'});
      $modalInstance.close();
    });
  };

  $scope.apply = function(){
    if($scope.iteminfo.documentlist.length == 0){
      console.log($scope.iteminfo.documentlist);
    } else {
      $scope.flag = true;
    }
  };

  $scope.change = function(obj2, obj){
    var index = $scope.formdate.param_index[obj.value+'_'+obj2.sel_cid];
    if(index){
      var temp = obj.param[index];
      var para = {
        'pid': obj2.value1,
        'idcardNumber': $stateParams.cardno
      }
      $resource('/api/ac/csc/createPostService/getAdministrativePlanning', {}, {}).save(para,function (res) {
        if (res.errcode !== 0) {
          toaster.error({title: '', body: '查询区域信息失败'});
          return;
        }
        temp.select = res.data.data.divisions;
      });
    }
  };

  $scope.back = function(){
    $scope.flag = false;
  };

  

  

  
}